using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Driver.Application;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;

namespace NorthStar.Driver
{
    [Activity(Label = "New Work Available")]
    public class NewWorkAvailableActivity : Activity
    {
        public string driverId;
        public string bookingId;
        public string fullAddress;
        public string customerPhoneNumber;

        private Timer jobTimer;
        private int timerCount = 9;
        private TextView jobTimerLabel;

        protected override void OnCreate(Bundle bundle)
        {
            
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.NewJobAvailable);

            fullAddress = Intent.GetStringExtra("fullAddress");
            customerPhoneNumber = Intent.GetStringExtra("customerPhoneNumber");
            driverId = Intent.GetStringExtra("driverID");
            bookingId = Intent.GetStringExtra("bookingId");

            TextView jobLocationLabel = FindViewById<TextView>(Resource.Id.newJobLocation);
            jobLocationLabel.Text = string.Format("at {0}", Intent.GetStringExtra("jobLocation"));

            jobTimerLabel = FindViewById<TextView>(Resource.Id.newJobTimerText);

            Button acceptBtn = FindViewById<Button>(Resource.Id.AcceptWorkBtn);
            acceptBtn.Click += (sender, args) =>
                                   {
                                       jobTimer.Stop();
                                       AcceptBookingAsync asyncTask = new AcceptBookingAsync(this);

                                       asyncTask.Execute();

                                     
                                   };

            Button rejectBtn = FindViewById<Button>(Resource.Id.RejectWorkBtn);
            rejectBtn.Click += (sender, args) => { Close(); };

            jobTimer = new Timer(1000);
            jobTimer.Elapsed += (sender, e) =>
                                    {
                                        CheckTimer();
            
                                    };
            jobTimer.Start();
          
           
            // Create your application here
        }

      
        private void Close()
        {
            jobTimer.Stop();
            Intent a = new Intent(this, typeof(MainActivity));
            a.PutExtra("driverID", driverId);
            a.AddFlags(ActivityFlags.ClearTop);
            StartActivity(a);
        }

        private void CheckTimer()
        {
            if(timerCount == 0)
            {
                //timeout so return home
                Close();
            }
            else
            {
                timerCount--;
                RunOnUiThread(() => { jobTimerLabel.Text = string.Format("{0} seconds to respond", timerCount); });

            }
        }
    }
}